package com.bw.controller;

import com.alibaba.fastjson.JSONObject;
import com.bw.pojo.User;
import com.bw.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user/*") //模块名 /具体接口名
public class UserController extends BaseController {
    UserServiceImpl service = new UserServiceImpl();
    //登录方法（接口）
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1,接收参数
        String reqInfos = req.getParameter("reqInfos");
        //将json数据转对象
        User user = JSONObject.parseObject(reqInfos, User.class);
        //3.将接受的对象 丢给 Service 处理
        int i = service.login(user,req,resp); //状态1账号错误 2密码错误 3账号禁用 4登录成功
        //4.将结果响应给用户
        resp.getWriter().println(JSONObject.toJSONString(i));
    }
    //用户列表
    protected void getUserList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数
        String reqInfos = req.getParameter("reqInfos");
        //将JSON数据传对象
        User user = JSONObject.parseObject(reqInfos, User.class);
        //将对象传给Service
        List<User> list = service.getUserList(user);
        //将结果响应给用户
        resp.getWriter().println(JSONObject.toJSONString(list));
    }

    //添加
    protected void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接受参数
        String reqInfos = req.getParameter("reqInfos");
        //2.将JSON数据传对象
        User user = JSONObject.parseObject(reqInfos, User.class);
        //3.将对象传给Service
        service.addUser(user);
        //4.将结果响应给用户
        resp.getWriter().println(JSONObject.toJSONString(200));
    }

    //删除
    protected void delUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接受参数
        String reqInfos = req.getParameter("reqInfos");
        //2.将JSON数据传对象
        User user = JSONObject.parseObject(reqInfos, User.class);
        //3.将对象传给Service
        service.delUser(user.getUids());//删除需要传递 uids 拼接好的字符串 id
        //4.将结果响应给用户
        resp.getWriter().println(JSONObject.toJSONString(200));
    }

    //回显
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1,接收参数
        String reqInfos = req.getParameter("reqInfos");
        //2.将json数据转对象
        User user = JSONObject.parseObject(reqInfos, User.class);
        //3.将接受的对象 丢给 Service 处理
        User u = service.finById(user.getUid());
        //4.将结果响应给用户
        resp.getWriter().println(JSONObject.toJSONString(u));
    }

    //修改
    protected void updUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String reqInfos = req.getParameter("reqInfos");
        //2.将JSON数据传对象
        User user = JSONObject.parseObject(reqInfos, User.class);
        //3.将对象传给Service
        service.updUser(user);//删除需要传递 uids 拼接好的字符串 id
        //4.将结果响应给用户
        resp.getWriter().println(JSONObject.toJSONString(200));
    }

    //退出系统
    protected void exit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //清楚session
        req.getSession().removeAttribute("user");
    }
}
